package com.e3roid.examples;

import java.util.ArrayList;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Toast;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Menu;
import com.e3roid.drawable.Shape;
import com.e3roid.drawable.modifier.AlphaModifier;
import com.e3roid.drawable.modifier.AxisMoveModifier;
import com.e3roid.drawable.modifier.DelayModifier;
import com.e3roid.drawable.modifier.ProgressModifier;
import com.e3roid.drawable.modifier.SequenceModifier;
import com.e3roid.drawable.modifier.ShapeModifier;
import com.e3roid.drawable.modifier.SpanModifier;
import com.e3roid.drawable.modifier.function.Linear;
import com.e3roid.drawable.sprite.AnimatedSprite;
import com.e3roid.drawable.sprite.TextSprite;
import com.e3roid.drawable.texture.TiledTexture;

public class MenuExitPauseExample extends E3Activity {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private TextSprite playText;
	private TextSprite exitText;

	private TiledTexture    kingTexture;
	private AnimatedSprite  king;
	private Menu menu;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.setBackgroundColor(0.97f, 0.97f, 1, 1);
		
		// set animated sprite to the scene with move modifier.
		king = new AnimatedSprite(kingTexture, getCenterX(kingTexture), 0);
		king = addMoveModifier(animate(king));
		scene.getTopLayer().add(king);
		
		scene.addEventListener(playText);
		scene.addEventListener(exitText);
		
		// create menu
		menu = new Menu();
		menu.add(playText);
		menu.add(exitText);
		
		// layout menu to vertical and aligned to center.
		menu.layoutVerticalCenter(this);
		scene.addMenu(menu);
		
		Toast.makeText(this, "Press menu key to show menu", Toast.LENGTH_LONG).show();
		return scene;
	}

	@Override
	public void onLoadResources() {
		kingTexture = new TiledTexture("king.png", 31, 49, 0, 0, 3, 2, this);
		
		// create menu text and define touch event.
		playText = new TextSprite("PLAY", 18, Color.BLUE, Color.GRAY, 50, Typeface.DEFAULT, this) {
			private boolean modifying = false;
			@Override
			public boolean onTouchEvent(E3Scene scene, Shape shape,
					MotionEvent motionEvent, int localX, int localY) {
				if (modifying) return false;
				if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
					SpanModifier modifier = new SpanModifier(3, 200L, new AlphaModifier(0, 0, 1), this);
					modifier.passValue(scene);
					playText.addModifier(modifier);
					modifying = true;
					return true;
				}
				return false;
			}
			@Override
			public void onModifierFinished(ShapeModifier modifier, Shape shape) {
				E3Scene scene = (E3Scene)modifier.receiveValue();
				scene.resetNamedLayer();
				scene.resume();
				modifying = false;
			}
		};
		exitText = new TextSprite("EXIT", 18, Color.RED,  Color.GRAY, 50, Typeface.DEFAULT, this) {
			@Override
			public boolean onTouchEvent(E3Scene scene, Shape shape,
					MotionEvent motionEvent, int localX, int localY) {
				if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
					exitText.addModifier(new SpanModifier(3, 200L, new AlphaModifier(0, 0, 1), this));
					return true;
				}
				return false;
			}
			@Override
			public void onModifierFinished(ShapeModifier modifier, Shape shape) {
				finish();
			}
		};
	}

	private int getCenterX(TiledTexture texture) {
		return (getWidth() - texture.getTileWidth()) / 2;
	}
	
	// Add animation frames to the sprite
	private AnimatedSprite animate(AnimatedSprite sprite) {
		ArrayList<AnimatedSprite.Frame> frames = new ArrayList<AnimatedSprite.Frame>();
		frames.add(new AnimatedSprite.Frame(0, 0));
		frames.add(new AnimatedSprite.Frame(1, 0));
		frames.add(new AnimatedSprite.Frame(2, 0));
		frames.add(new AnimatedSprite.Frame(3, 0));
		
		sprite.animate(500, 0, frames);
		return sprite;
	}

	// Add move modifier to the sprite
	private AnimatedSprite addMoveModifier(AnimatedSprite sprite) {
		float toY = getHeight() - sprite.getHeight();
		SequenceModifier modifier = new SequenceModifier(
				new DelayModifier(1000),
				new ProgressModifier(
						new AxisMoveModifier(0, 0, toY, AxisMoveModifier.AXIS_Y),
				5000, Linear.getInstance()));
		modifier.setLoop(true);
		sprite.addModifier(modifier);
		
		return sprite;
	}
	
	@Override
	public boolean onKeyDown(E3Scene scene, int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_DOWN) {
			if (scene.isMenuVisible()) {
				scene.hideMenu();
				scene.resume();
			} else {
				scene.showMenu();
				scene.pause();
			}
			return true;
		} else {
			return super.onKeyDown(scene, keyCode, event);
		}
	}
}
